package com.ccExcelUtil.validator.relatedSheet;

import com.ccExcelUtil.service.ValidatedRelatedSheetService;
import com.ccExcelUtil.util.ContextHolder;
import com.ccExcelUtil.util.ViolationUtils;
import com.ccExcelUtil.validator.ValidatorUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.List;
import java.util.Map;

public class RelatedSheetValidator implements ConstraintValidator<RelatedSheet, Object> {

    ViolationUtils validatorUtils;

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
        validatorUtils = new ViolationUtils(constraintValidatorContext);
        Map<Integer, List<?>> dataMap = ContextHolder.getDataMap();
        ValidatedRelatedSheetService<Object> validatedRelatedSheetService = ValidatorUtils.VALIDATED_RELATED_SHEET_SERVICE_MAP.get(value.getClass().getName());
        if (validatedRelatedSheetService == null) {
            // 如果没有找到对应的服务，返回true（跳过校验）
            return true;
        }
        return validatedRelatedSheetService.validator(dataMap, value);
    }
}
